﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernateDemo.DAL.Entity;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Transform;
using Arich.Data.GenericNHDal;

namespace NHibernateDemo.DAL {
    public class FundBasicDAL:AbstractDataManageDal<FundBasic,string> {



    }


    public class CategoryDAL : AbstractDataManageDal<Category, string> {

        

        public IList<Category> SearchByName(string name) {
            return this.NHibernateSession.CreateSQLQuery(@"select distinct {category.*} from test.category {category} 
                inner join test.fundbasic f on f.CategoryId = {category}.CategoryId 
                where {category}.Name = :name")
                .AddEntity("category", typeof(Category))
                .SetString("name", name)
                .List<Category>( );
        }

        public IList<Category> SearchByLikeName(string name) {
            //simple
            //return NHibernateSession.CreateCriteria<Category>( )
            //            .Add(Restrictions.Like("Name", name,MatchMode.Anywhere))
            //            .CreateCriteria("Funds")
            //            .List<Category>( );
            //distinct
            return NHibernateSession.CreateCriteria<Category>( )
                        .Add(Restrictions.Like("Name", name, MatchMode.Anywhere))
                        .CreateCriteria("Funds")
                        .SetResultTransformer(new DistinctRootEntityResultTransformer()) 
                        .List<Category>( );
        }
    }
}
